﻿//-----------------------------------------------------------------------------------------------------------
//  Description:
//      企业相关
//      
//      
//  Comment                          Date                 Author      Email
//   ----------------------------    -------             -------      -----------------------------------
//  Create                           2012/11/16           wukun       17622690@qq.com
//
// 
//-----------------------------------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Forest.Core.Data;
using Forest.Cat.Domain.Entities;
using Forest.Cat.Web.Models;
using Forest.Cat.Domain.Services.Compnaies;
using Forest.Cat.Domain.Services.Pictures;
using Forest.Cat.Web.Extensions;
using Forest.Cat.Domain.Configuration;
using Forest.Cat.Web.Models.Catalog;
using Forest.Cat.Domain.Services.Catalog;

namespace Forest.Cat.Web.Controllers {
    public class CompanyController : Controller {
        #region Fields
        private readonly IJobPositionService _jobPositionService;
        private readonly ILovService _lovService;
        private readonly ICompanyService _companyService;
        private readonly IPictureService _pictureService;
        #endregion

        #region Constructures
        public CompanyController(
            ILovService lovService,
            IJobPositionService jobPositionService,
            ICompanyService companyService,
            IPictureService pictureService) {
            _lovService = lovService;
            _jobPositionService = jobPositionService;
            _companyService = companyService;
            _pictureService = pictureService;
        }
        #endregion

        #region Methods
        #region CompanyView
        public ActionResult CompanyView(Guid id) {
            var company = _companyService.GetCompanyById(id);
            if (company != null && company.Status ==1) {
                var companyModel = PrepareCompanyModelForView(company);
                //为网址带上头信息
                if (!String.IsNullOrWhiteSpace(companyModel.WebSite) && !companyModel.WebSite.ToLower().Contains("http")) {
                    companyModel.WebSite = "http://" + companyModel.WebSite;
                }
                var template = _companyService.GetTemplate(company.TemplateId.Value);

                return View(template.ViewPath, companyModel);
            }
            else {
                return RedirectToAction("NoInfo", "Home");
                
            }
        }

        [NonAction]
        private CompanyModel PrepareCompanyModelForView(Company company) {
            var companyModel = company.ToModel();
            companyModel.CompanyTypeName = _lovService.GetLovName(company.CompanyType.Value);
            //companyModel.EmployeesName = _lovService.GetLovName(company.Employees.Value);
            companyModel.Employees = company.Employees == null ? 0 : company.Employees;
            companyModel.IndustryName = _lovService.GetLovName(company.Industry.Value);
            //获取Logo
            var logoInfoId = _pictureService.GetCompanyPicturesId(company.Id, true).Select(x => (Guid?)x).FirstOrDefault();
            companyModel.CompanyLogoId = logoInfoId;
            //获取公司图片
            companyModel.PictureIdList = _pictureService.GetCompanyPicturesId(company.Id, false);

            //var jobList = _jobPositionService.GetJobListByCompanyId(company.Id).Take(15).ToList();
            var jobList = _jobPositionService.GetJobListByCompanyId(company.Id).ToList();
            companyModel.JobPositionList = jobList.Select(x => PrepareJobModelForView(x)).ToList();

            return companyModel;
        }

     
        public ActionResult CompanyPositionList(Guid companyId) {
            var jobList = _jobPositionService.GetJobListByCompanyId(companyId).ToList();

            return PartialView( jobList.Select(x => PrepareJobModelForView(x)).ToList());
        }

        [NonAction]
        private JobPositionModel PrepareJobModelForView(JobPosition job) {
            var jobModel = job.ToModel();
            jobModel.YearsOfExperienceName = _lovService.GetLovName(job.YearsOfExperience.Value);
            jobModel.EducationLevelName = _lovService.GetLovName(job.EducationLevel.Value);
            jobModel.CityName = _lovService.GetLovName(job.City);
            //jobModel.DistrictName = job.District.HasValue ? _lovService.GetLovName(job.District.Value) : String.Empty;
            return jobModel;
        }
        #endregion

        #region GetImage
        public FileContentResult GetImage(Guid id) {
            var picture = _pictureService.GetPictureById(id);
            if (picture != null) {
                return File(picture.PictureBinary, String.IsNullOrWhiteSpace(picture.MimeType) ? "image/jpeg" : picture.MimeType);
            }
            else {
                return null;
            }
        }
        #endregion
        #endregion
    }
}
